package com.zhuiyun.project.api.complaint.service.impl;

import com.github.pagehelper.PageHelper;
import com.zhuiyun.project.api.ancientPoetry.entity.AncientPoetry;
import com.zhuiyun.project.api.complaint.entity.Complain;
import com.zhuiyun.project.api.complaint.entity.ComplaintAndUser;
import com.zhuiyun.project.api.complaint.mapper.ComplaintMapper;
import com.zhuiyun.project.api.complaint.model.Complaintmodel;
import com.zhuiyun.project.api.complaint.service.ComplaintService;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.BeanUtils;
import com.zhuiyun.project.utils.ObjectUtils;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import com.zhuiyun.project.utils.pageUtil.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName ComplaintServiceImpl
 * @Description TODO
 * @Author DF
 * @Date 2023/12/13 15:03
 **/
@Service
public class ComplaintServiceImpl implements ComplaintService {
    @Autowired
    private ComplaintMapper complaintMapper;
    /**
    *
    * @Author jxz
    * @Description 投诉的全查方法
    * @Date 2023/12/13 19:40
    * @Return Commonresult
    * @param complaintmodel
    **/
    @Override
    public CommonResult SelectAll(Complaintmodel complaintmodel, PageRequest pageRequest) {
        Complain complain = new Complain();
        BeanUtils.copy(complaintmodel, complain);
        PageHelper.startPage(pageRequest);
//        Example ex=new Example(Complaint.class);
//        ex.createCriteria().andEqualTo("delFlag",0);
//        List<Complaint> list=complaintMapper.selectByExample(ex);
        List<ComplaintAndUser> list = complaintMapper.selectall(complain);
        PageResult pageResult= MybatisPageHelper.getPageResult(list);
        if(ObjectUtils.isNotEmpty(pageResult)){
            return CommonResult.ok(pageResult);
        }else {
            return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
        }
    }

    @Override
    public CommonResult Selectone(Complaintmodel complaintmodel, PageRequest pageRequest) {
        Complain complain = new Complain();
        BeanUtils.copy(complaintmodel, complain);
        Example example = new Example(Complain.class);
        if(ObjectUtils.isNotEmpty(complain.getId())){
            example.createCriteria().andEqualTo("id",complain.getId());
        }//注入example条件
        example.createCriteria().andEqualTo("delFlag",0);//注入example条件
        ComplaintAndUser complaintAndUser = complaintMapper.selectne(complain);
        complain = new Complain();
        BeanUtils.copy(complaintAndUser, complain);
        if (complain.getReadFlag() != 1){
            complain.setReadFlag(1);
            System.out.println(complaintAndUser);
            int i = complaintMapper.updateByExample(complain,example);
            System.out.println(complaintAndUser);
            if(ObjectUtils.isNotEmpty(complaintAndUser) && i>0){
                return CommonResult.ok(complaintAndUser);
            }else {
                return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
            }
        }
        if(ObjectUtils.isNotEmpty(complaintAndUser)){
            return CommonResult.ok(complaintAndUser);
        }else {
            return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
        }
    }
}
